rm(list = ls())

## 

library(tidyverse)
library(rdrobust)

## Get federal election data

bt <- readRDS("data/data_federal.rds") %>%
  filter(!is.na(treated))

## List of outcomes

outcomes <- c("turnout_party", 
              "agg_left_party", 
              "agg_center_party", 
              "agg_right_party")

## Drop 2009

bt <- bt %>% 
  filter(year > 2012)

## First differences

diff_df <- pblapply(outcomes, function(o) {
  out <- bt %>%
    filter(year > 2012) %>%
    pivot_wider(values_from = o, names_from = 'year', id_cols = 'ags',
                names_prefix = 'o') %>%
    mutate(diff = o2017  - o2013) %>%  dplyr::select(ags, diff) 
  ## Rename
  colnames(out)[2] <- o
  
  ## Return this
  out
}) %>%
  reduce(left_join) %>%
  left_join(bt %>% dplyr::select(ags, pop_dec_09, applies_census, state_id) %>%
              distinct(ags, .keep_all = T)) %>%
  mutate(runvar = (pop_dec_09 * -1) + 10000) %>%
  filter(applies_census == 1)

## BW=2500

bw_use = 2500

## Subset: exclude B-W

subset_select <- !diff_df$state_id == '08' & between(diff_df$pop_dec_09, 
                                                     10000 - bw_use, 
                                                     10000 + bw_use) 

## Make plots
## Centrist parties

out_center <- rdrobust::rdplot(y = diff_df[subset_select, ] %>% 
                                 pull(agg_center_party), 
                               x = diff_df$pop_dec_09[subset_select], 
                               nbins = 10,
                               c = 10000, 
                               h = bw_use,
                               col.lines = 'black',
                               kernel = 'triangular',
                               p = 3, x.label = 'Pre-census\npopulation (2009)', 
                               y.label = "Change in center vote shares\nbetween 2013 and 2017\n(binned, p.p.)",
                               title = 'Center parties')
out_center <- last_plot() +theme_bw()

## Left-leaning parties

out_left <- rdrobust::rdplot(y = diff_df[subset_select, ] %>% 
                               pull(agg_left_party), 
                             x = diff_df$pop_dec_09[subset_select], 
                             nbins = 10,
                             c = 10000, 
                             h = bw_use,
                             col.lines = 'black',
                             kernel = 'triangular',
                             p = 3, x.label = 'Pre-census\npopulation (2009)', 
                             y.label = "Change in left-wing vote shares\nbetween 2013 and 2017\n(binned, p.p.)",
                             title = 'Left-wing parties')
out_left <- last_plot() +theme_bw()

## Right-leaning parties

out_right <- rdrobust::rdplot(y = diff_df[subset_select, ] %>% 
                                pull(agg_right_party), 
                              x = diff_df$pop_dec_09[subset_select], 
                              nbins = 10,
                              c = 10000, 
                              h = bw_use,
                              kernel = 'triangular',
                              p = 3, 
                              col.lines = 'black',
                              x.label = 'Pre-census\npopulation (2009)', 
                              y.label = "Change in right-wing vote shares\nbetween 2013 and 2017\n(binned, p.p.)",
                              title = 'Right-wing parties')
out_right <- last_plot() +theme_bw()

# Figure A.7: RD plots ----

cowplot::plot_grid(out_left, out_center, out_right, ncol = 3, align = 'hv')

